#!/bin/bash
# chown -R 在 vfs 层是逐个文件传给 fs 进行处理
WHOLEQUOTA="/${QUOTA_TEST}/wholeQuotaDir"
USERQUOTA="/${QUOTA_TEST}/userQuotaDir"
LOGFILE="/tmp/quotalog"
WHOLEDIR="${FS_MOUNTPOINT}${WHOLEQUOTA}"
USERDIR="${FS_MOUNTPOINT}${USERQUOTA}"
global_used=0

SRC_USER="u1"; SRC_GROUP="u1"
SRC_UID=`cat /etc/passwd | grep ${SRC_USER} | awk -F: '{print $3}'`
SRC_GID=`cat /etc/passwd | grep ${SRC_USER} | awk -F: '{print $4}'`

DEST_USER="u2"; DEST_GROUP="u2"
DEST_UID=`cat /etc/passwd | grep ${DEST_USER} | awk -F: '{print $3}'`
DEST_GID=`cat /etc/passwd | grep ${DEST_USER} | awk -F: '{print $4}'`

NOQUOTA_USER="u3"; NOQUOTA_GROUP="u3"
NOQUOTA_UID=`cat /etc/passwd | grep ${NOQUOTA_USER} | awk -F: '{print $3}'`
NOQUOTA_GID=`cat /etc/passwd | grep ${NOQUOTA_USER} | awk -F: '{print $4}'`

global_src_uid_used=0; global_dest_uid_used=0
global_src_gid_used=0; global_dest_gid_used=0


msg0="测试所有用例"
msg1="配额类型是目录配额时，配额不变"
msg2="配额类型是 UID 时, 原 uid 和新 uid 都配置了配额，验证配额没有超出"
msg3="配额类型是 UID 时, 原 uid 和新 uid 都配置了配额，验证配额超出"
msg4="配额类型是 UID 时, 原 uid 设置配额，新 uid 没有设置配额, 配额不会超出"
msg5="配额类型是 UID 时, 原 uid 没有设置配额，新 uid 设置配额, 验证配额超出"
msg6="配额类型是 UID 时, 原 uid 没有设置配额，新 uid 设置配额, 验证配额没有超出"
msg7="配额类型是 UID 时, 原 uid 和新 uid 都没有设置配额"
msg8="配额类型是 GID 时, 原 gid 和新 gid 都配置了配额，验证配额没有超出"
msg9="配额类型是 GID 时, 原 gid 和新 gid 都配置了配额，验证配额超出"
msg10="配额类型是 GID 时, 原 gid 设置配额，新 gid 没有设置配额, 配额不会超出"
msg11="配额类型是 GID 时, 原 gid 没有设置配额，新 gid 设置配额, 验证配额超出"
msg12="配额类型是 GID 时, 原 gid 没有设置配额，新 gid 设置配额, 验证配额没有超出"
msg13="配额类型是 GID 时, 原 gid 和新 gid 都没有设置配额"
maxmsg=13

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function reset_all() {
    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    
    rm -rf ${WHOLEDIR} ${USERDIR}; mkdir -p ${WHOLEDIR} ${USERDIR}
    echo -e "=====清空目录并重新创建目录===== pass"

    orcafs-ctl --setquota --dir=${WHOLEDIR} --hardlimit=10M &> /dev/null
    orcafs-ctl --setquota --dir=${USERDIR} --uid=${SRC_UID} --hardlimit=10M &> /dev/null
    orcafs-ctl --setquota --dir=${USERDIR} --uid=${DEST_UID} --hardlimit=5M &> /dev/null
    orcafs-ctl --setquota --dir=${USERDIR} --gid=${SRC_GID} --hardlimit=10M  &> /dev/null
    orcafs-ctl --setquota --dir=${USERDIR} --gid=${DEST_GID} --hardlimit=5M &> /dev/null
    sleep ${WAIT_CLIENT_SYNC};
    echo -e "=====重新创建 ${WHOLEQUOTA} ${USERQUOTA} 目录配额===== pass"
}

function func_get_src_uid_used() {
    used=`orcafs-ctl --listquota | grep -E -w "${USERQUOTA}" | grep DIR_UID | grep ${SRC_UID} | awk '{print $9}'`
    echo -n -e "before chown (uesr ${SRC_USER}: ${used}):\n [ls ${USERDIR}]: "; ls -l ${USERDIR}
    global_src_uid_used=$used
}

function func_get_dest_uid_used() {
    used=`orcafs-ctl --listquota | grep -E -w "${USERQUOTA}" | grep DIR_UID | grep ${DEST_UID} | awk '{print $9}'`
    echo -n -e "after chown (uesr ${DEST_USER}: ${used}):\n [ls ${USERDIR}]: "; ls -l ${USERDIR}
    global_dest_uid_used=$used
}

function func_get_src_gid_used() {
    used=`orcafs-ctl --listquota | grep -E -w "${USERQUOTA}" | grep DIR_GID | grep ${SRC_GID} | awk '{print $9}'`
    echo -n -e "before chown (group ${SRC_GROUP}: ${used}):\n [ls ${USERDIR}]: "; ls -l ${USERDIR}
    global_src_gid_used=$used
}

function func_get_dest_gid_used() {
    used=`orcafs-ctl --listquota | grep -E -w "${USERQUOTA}" | grep DIR_GID | grep ${DEST_GID} | awk '{print $9}'`
    echo -n -e "after chown (group ${DEST_GROUP}: ${used}):\n [ls ${USERDIR}]: "; ls -l ${USERDIR}
    global_dest_gid_used=$used
}

echo "/*******************************"
echo "     测试 chown 文件/目录        "
echo "*******************************/"
if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi
# init global quota env and check env
sh ${QUOTA_CHECK_ENV}
source ${QUOTA_COMMON_FUNC}
reset_all;

if (($1 == 1)) || (($1 == 0));then
    echo -e "\n==1===配额类型是目录配额时，配额不变"
    rm -rf ${WHOLEDIR}/*
    dd if=/dev/zero of=${WHOLEDIR}/file1 bs=2M count=1 &> /dev/null
    dd if=/dev/zero of=${WHOLEDIR}/file2 bs=1M count=1 &> /dev/null
    
    old_used=`orcafs-ctl --listquota | grep -E -w "${WHOLEQUOTA}" | awk '{print $8}'`
    echo -n -e "before chown(${old_used}):\n [ls ${WHOLEDIR}]: "; ls -l ${WHOLEDIR}
# test chown all dir
    cmd="chown -R ${DEST_USER}:${DEST_USER} ${WHOLEDIR}";echo -e "\nexec cmd:\n\t${cmd}\n"; eval ${cmd} &>${LOGFILE}
    new_used=`orcafs-ctl --listquota | grep -E -w "${WHOLEQUOTA}" | awk '{print $8}'`
    echo -n -e "after chown(${new_used}):\n [ls ${WHOLEDIR}]: "; ls -l ${WHOLEDIR}

    if ((${old_used} == ${new_used})); then echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 2)) || (($1 == 0));then
    echo -e "\n==2===配额类型是 UID 时, 原 uid 和新 uid 都配置了配额，验证配额没有超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=2M count=1 &> /dev/null # 配额不会增加，id不相符
    dd if=/dev/zero of=${USERDIR}/file2 bs=1M count=1 &> /dev/null

    chown -R ${SRC_USER}:${SRC_GROUP} ${USERDIR}
    func_get_src_uid_used; src_uid_used=${global_src_uid_used}

# uid test chown all dir
    cmd="chown -R ${DEST_USER} ${USERDIR}";echo -e "\nexec cmd:\n\t${cmd}\n"; eval ${cmd} &>${LOGFILE}
    
    func_get_dest_uid_used; dest_uid_used=${global_dest_uid_used}
    if ((${src_uid_used} == ${dest_uid_used})); then echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 3)) || (($1 == 0));then
    echo -e "\n==3===配额类型是 UID 时, 原 uid 和新 uid 都配置了配额，验证配额超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=6M count=1 &> /dev/null # 配额不会增加，id不相符

    chown -R ${SRC_USER}:${SRC_GROUP} ${USERDIR}
    func_get_src_uid_used; src_uid_used=${global_src_uid_used}

    cmd="chown -R ${DEST_USER}:${DEST_GROUP} ${USERDIR}";echo -e "\nexec cmd:\n\t${cmd}"; eval ${cmd} &>${LOGFILE}
    if (($? == 1)); then echo -e "\tDisk quota exceeded, 测试成功\n"; break; else echo -e "验证失败\n"; exit 1;fi

    func_get_dest_uid_used; dest_uid_used=${global_dest_uid_used}
    if ((${src_uid_used} == 6291456)) && (( ${dest_uid_used} == 0)); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 4)) || (($1 == 0));then
    echo -e "\n==4===配额类型是 UID 时, 原 uid 设置配额，新 uid 没有设置配额, 配额不会超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=2M count=1 &> /dev/null # 配额不会增加，id不相符
    dd if=/dev/zero of=${USERDIR}/file2 bs=1M count=1 &> /dev/null

    chown -R ${SRC_USER}:${SRC_GROUP} ${USERDIR}
    func_get_src_uid_used; old_src_uid_used=${global_src_uid_used}

# test chown one file
    cmd="chown -R ${NOQUOTA_USER}:${NOQUOTA_GROUP} ${USERDIR}";echo -e "\nexec cmd:\n\t${cmd}\n"; eval ${cmd} &>${LOGFILE}
    func_get_src_uid_used; new_src_uid_used=${global_src_uid_used}
    
    if ((${old_src_uid_used} == 3145728)) && (( ${new_src_uid_used} == 0)); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 5)) || (($1 == 0));then
    echo -e "\n==5===配额类型是 UID 时, 原 uid 没有设置配额，新 uid 设置配额, 验证配额超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=6M count=1 &> /dev/null # 配额不会增加，id不相符

# uid not set (u3)
    chown -R ${NOQUOTA_USER}:${NOQUOTA_GROUP} ${USERDIR}
    func_get_dest_uid_used; old_dest_uid_used=${global_dest_uid_used}

# test chown dir
    cmd="chown -R ${DEST_USER}:${DEST_GROUP} ${USERDIR}";echo -e -n "\nexec cmd:\n\t${cmd}\n\t"; eval ${cmd} 
    if (($? == 1)); then echo -e "\tDisk quota exceeded, 测试成功\n"; break; else echo "验证失败\n"; exit 1;fi
    
    func_get_dest_uid_used; new_dest_uid_used=${global_dest_uid_used}
    
    if ((${old_dest_uid_used} == 0)) && (( ${new_dest_uid_used} == 0)); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 6)) || (($1 == 0));then
    echo -e "\n==6===配额类型是 UID 时, 原 uid 没有设置配额，新 uid 设置配额, 验证配额没有超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=2M count=1 &> /dev/null # 配额不会增加，id不相符
    dd if=/dev/zero of=${USERDIR}/file2 bs=1M count=1 &> /dev/null
# uid not set (u3)
    chown -R ${NOQUOTA_USER}:${NOQUOTA_GROUP} ${USERDIR}
    func_get_dest_uid_used; old_dest_uid_used=${global_dest_uid_used}

# test chown dir
    cmd="chown -R ${DEST_USER}:${DEST_GROUP} ${USERDIR}";echo -e "\nexec cmd:\n\t${cmd}\n"; eval ${cmd} &>${LOGFILE}
    
    func_get_dest_uid_used; new_dest_uid_used=${global_dest_uid_used}
    
    if ((${old_dest_uid_used} == 0)) && (( ${new_dest_uid_used} == 3145728)); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 7)) || (($1 == 0));then
    echo -e "\n==7===配额类型是 UID 时, 原 uid 和新 uid 都没有设置配额"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=2M count=1 &> /dev/null # 配额不会增加，id不相符
    dd if=/dev/zero of=${USERDIR}/file2 bs=1M count=1 &> /dev/null

# uid not set (u3)
    chown -R ${NOQUOTA_USER}:${NOQUOTA_GROUP} ${USERDIR}
    old_used=`orcafs-ctl --listquota | grep -E -w "${USERQUOTA}" | grep ${NOQUOTA_UID}`
    if [ -z ${old_used} ];then old_used=0; fi
    echo -n -e "after chown (${NOQUOTA_USER}<not set>: ${old_used}):\n [ls ${USERDIR}]: "; ls -l ${USERDIR}
    if ((${old_used} == 0)); then echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

#********************************************
# test gid
#********************************************
if (($1 == 8)) || (($1 == 0));then
    echo -e "\n==8===配额类型是 GID 时, 原 gid 和新 gid 都配置了配额，验证配额没有超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=2M count=1 &> /dev/null # 配额不会增加，id不相符
    dd if=/dev/zero of=${USERDIR}/file2 bs=1M count=1 &> /dev/null

    chown -R :${SRC_GROUP} ${USERDIR}
    func_get_src_gid_used; src_gid_used=${global_src_gid_used}

# uid test chown all dir
    cmd="chown -R :${DEST_GROUP} ${USERDIR}";echo -e "\nexec cmd:\n\t${cmd}\n"; eval ${cmd} &>${LOGFILE}
    
    func_get_dest_gid_used; dest_gid_used=${global_dest_gid_used}
    if ((${src_gid_used} == 3145728)) && ((3145728 == ${dest_gid_used})); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 9)) || (($1 == 0));then
    echo -e "\n==9===配额类型是 GID 时, 原 gid 和新 gid 都配置了配额，验证配额超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=6M count=1 &> /dev/null # 配额不会增加，id不相符

    chown -R :${SRC_GROUP} ${USERDIR}
    func_get_src_gid_used; src_gid_used=${global_src_gid_used}

    cmd="chown -R :${DEST_GROUP} ${USERDIR}";echo -e "\nexec cmd:\n\t${cmd}\n"; eval ${cmd} &>${LOGFILE}
    if (($? == 1)); then echo -e "\tDisk quota exceeded, 测试成功"; break; else echo "验证失败"; exit 1;fi

    func_get_dest_gid_used; dest_gid_used=${global_dest_gid_used}
    if ((${src_gid_used} == 6291456)) && (( ${dest_gid_used} == 0)); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 10)) || (($1 == 0));then
    echo -e "\n==10===配额类型是 GID 时, 原 gid 设置配额，新 gid 没有设置配额, 配额不会超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=2M count=1 &> /dev/null # 配额不会增加，id不相符
    dd if=/dev/zero of=${USERDIR}/file2 bs=1M count=1 &> /dev/null

    chown -R :${SRC_GROUP} ${USERDIR}
    func_get_src_gid_used; old_src_gid_used=${global_src_gid_used}

    cmd="chown -R :${NOQUOTA_GID} ${USERDIR}";echo -e "\nexec cmd:\n\t${cmd}\n"; eval ${cmd} &>${LOGFILE}
    
    func_get_src_gid_used; new_src_gid_used=${global_src_gid_used}
    if ((${old_src_gid_used} == 3145728)) && ((${new_src_gid_used} == 0)); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 11)) || (($1 == 0));then
    echo -e "\n==11===配额类型是 GID 时, 原 gid 没有设置配额，新 gid 设置配额, 验证配额超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=6M count=1 &> /dev/null # 配额不会增加，id不相符

# uid not set (u3)
    chown -R ${NOQUOTA_UID}:${NOQUOTA_GID} ${USERDIR}
    func_get_dest_gid_used; old_dest_gid_used=${global_dest_gid_used}

# test chown dir
    cmd="chown -R :${DEST_GROUP} ${USERDIR}";echo -e -n "\nexec cmd:\n\t${cmd}\n\t"; eval ${cmd} 
    if (($? == 1)); then echo -e "\tDisk quota exceeded, 测试成功\n"; break; else echo "验证失败"; exit 1;fi
    
    new_used=`orcafs-ctl --listquota | grep -E -w "${USERQUOTA}"| grep ${DEST_UID} | awk '{print $9}'`
    func_get_dest_gid_used; new_dest_gid_used=${global_dest_gid_used}
    
    if ((${old_dest_gid_used} == 0)) && ((${new_dest_gid_used} == 0)); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 12)) || (($1 == 0));then
    echo -e "\n==12===配额类型是 GID 时, 原 gid 没有设置配额，新 gid 设置配额, 验证配额没有超出"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=2M count=1 &> /dev/null # 配额不会增加，id不相符
    dd if=/dev/zero of=${USERDIR}/file2 bs=1M count=1 &> /dev/null

# uid not set (u3)
    chown -R ${NOQUOTA_UID}:${NOQUOTA_GID} ${USERDIR}
    func_get_dest_gid_used; old_dest_gid_used=${global_dest_gid_used}

# test chown dir
    cmd="chown -R :${DEST_GROUP} ${USERDIR}";echo -e "\nexec cmd:\n\t${cmd}\n"; eval ${cmd} &>${LOGFILE}
    
    func_get_dest_gid_used; new_dest_gid_used=${global_dest_gid_used}

    if ((${old_dest_gid_used} == 0)) && ((${new_dest_gid_used} == 3145728)); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

if (($1 == 13)) || (($1 == 0));then
    echo -e "\n==13===配额类型是 GID 时, 原 gid 和新 gid 都没有设置配额"
    rm -rf ${USERDIR}/*
    dd if=/dev/zero of=${USERDIR}/file1 bs=2M count=1 &> /dev/null # 配额不会增加，id不相符
    dd if=/dev/zero of=${USERDIR}/file2 bs=1M count=1 &> /dev/null

# uid not set (u3)
    chown -R :${NOQUOTA_GROUP} ${USERDIR}
    new_used=`orcafs-ctl --listquota | grep -E -w "${USERQUOTA}" | grep ${NOQUOTA_UID}`
    if [ -z ${new_used} ];then new_used=0; fi
    echo -n -e "after chown (${NOQUOTA_USER}<not set>: ${new_used}):\n [ls ${USERDIR}]: "; ls -l ${USERDIR}
    if ((${new_used} == 0)); then echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
fi

echo "/*******************************"
echo " 测试 chown 文件/目录 all success"
echo "*******************************/"
exit 0;
